import java.util.LinkedList;

public class PERTnetwerk {
	private Position finish;
	private LinkedList<State> partial_solution = null;
	LinkedList<Position> toVisited = new LinkedList<Position>();
	private GenericGraph<Position, Integer> my_netwerk = new GenericGraph<Position, Integer>();


	public void setGraph(GenericGraph<Position, Integer> netwerk) {
		my_netwerk = netwerk;
	}

	public LinkedList<State> findWay(State current, Position finish) {
		LinkedList<State> visited = new LinkedList<State>();
		return findWay(current, finish, visited);
	}

	/**
	 * Deze methode vind de weg naar het eindpad middels breadth-first methode
	 * @param current
	 * @param finish
	 * @param visited
	 * @return visited
	 */
	public LinkedList<State> findWay(State current, Position finish, LinkedList<State> visited) {
		visited.add(current);

		if (!toVisited.isEmpty()) {
			toVisited.remove(0);
			System.out.println("tov  " + toVisited.toString());
		}
		
		this.finish = finish;
		//TODO: check op finish-position
		//System.out.println("Huidig: " + current);

		//System.out.println("buren"+ my_netwerk.getNeighbors(current.getPosition1()));

			for (Position p : my_netwerk.getNeighbors(current.getPosition())) {
				
				System.out.println("Hier ben ik nu: " + p);
				
				p.setVoorganger(current.getPosition());
				
				System.out.println("Kosten van : " +p.getNaam() + " en " + current.getPosition() + " is :"	+ my_netwerk.getCost(current.getPosition(), p));
				p.setVroegsteTijd(my_netwerk.getCost(current.getPosition(), p));				
				
				for (State s : visited) {
					if (!s.getPosition().getAdded()) {
						s.getPosition().setAdded(true);
						toVisited.add(p);
					}
					
				}

			}
			System.out.println("hier ben i kgeweest " + visited+"\n\n");
			System.out.println("hier moet ik nog heen " + toVisited+"\n\n");
		
		if (!toVisited.isEmpty()) {
			return findWay(new State(toVisited.get(0)), finish, visited);
		} else {
			return visited;
		}

	}
}
